package com.whut.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whut.mapper.ContracttaskprojectMapper;
import com.whut.mapper.GroupMemberDetailMapper;
import com.whut.mapper.UserinfoMapper;
import com.whut.mapper.VerificationcodeMapper;
import com.whut.model.Userinfo;
import com.whut.model.Verificationcode;
import com.whut.service.UserService;
import com.whut.utils.SendSms;
import com.whut.utils.MD5Encoder;
import com.whut.v_model.GroupMemberDetail;
import org.apache.ibatis.jdbc.Null;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {


    @Resource
    private UserinfoMapper userinfoMapper;
    @Resource
    private GroupMemberDetailMapper groupMemberDetailMapper;
    @Resource
    private VerificationcodeMapper verificationcodeMapper;

    @Override
    public Userinfo getUserById(int id) {
        return userinfoMapper.selectByPrimaryKey(id);
    }


    @Override
    public String addUser(Userinfo userinfo) {
        try {
            userinfoMapper.insert(userinfo);
            System.out.println(userinfo);
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    @Override
    public Object getAllMembers(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<GroupMemberDetail> list = groupMemberDetailMapper.selectAll();
        PageInfo result = new PageInfo(list);
        return result;
    }



    @Override
    public Object multiSelct(int id, String sex) {
        return userinfoMapper.selectMultiParam(id,sex);
    }


    @Override
    public String registerWithSMS(String userPhone) {
        //检验是否存在此用户的手机号
        Userinfo userinfo = userinfoMapper.selectByUserPhone(userPhone);
        if(userinfo==null){
            //不存在此用户 允许注册
            JSONObject jsonObject = SendSms.send(userPhone);
            if("OK".equals(jsonObject.getString("Message"))){
                String v_code = jsonObject.getString("v_code");
                Verificationcode verificationcode = new Verificationcode();
                verificationcode.setPhone(userPhone);
                verificationcode.setTime(Long.toString(System.currentTimeMillis()));
                verificationcode.setV_code(v_code);

                if(verificationcodeMapper.selectByPrimaryKey(userPhone)!=null){
                    //更新
                    verificationcodeMapper.updateByPrimaryKey(verificationcode);
                }else{
                    //插入
                    verificationcodeMapper.insert(verificationcode);
                }
                return "success";
            }else{
                return "error";
            }
        }else{
            //存在此手机号，不允许注册
            return "repetitive_phone";
        }
    }

    @Override
    public String loginWithSMS(String userPhone) {
        //检验是否存在此用户的手机号
        Userinfo userinfo = userinfoMapper.selectByUserPhone(userPhone);
        if(userinfo!=null){
            //存在此用户 发短信
            JSONObject jsonObject = SendSms.send(userPhone);
            if("OK".equals(jsonObject.getString("Message"))){
                String v_code = jsonObject.getString("v_code");
                Verificationcode verificationcode = new Verificationcode();
                verificationcode.setPhone(userPhone);
                verificationcode.setTime(Long.toString(new Date().getTime()));
                verificationcode.setV_code(v_code);

                if(verificationcodeMapper.selectByPrimaryKey(userPhone)!=null){
                    //更新
                    verificationcodeMapper.updateByPrimaryKey(verificationcode);
                }else{
                    //插入
                    verificationcodeMapper.insert(verificationcode);
                }
                return "success";
            }else{
                return "error";
            }
        }else{
            return "no_this_user";
        }
    }

    @Override
    public Object checkLoginWithSMS(String userPhone, String v_code) {
        long curTime = System.currentTimeMillis();
        Verificationcode verificationcode = verificationcodeMapper.selectByPrimaryKey(userPhone);
        long lastTime = Long.parseLong(verificationcode.getTime());

        JSONObject jsonObject = new JSONObject();

        if(curTime-lastTime<180000){
            //时间间隔不超过180s
            //验证有效
            //进行正确性验证
            if (v_code.equals(verificationcode.getV_code())){
                //认证成功
                jsonObject.put("message","success");
                jsonObject.put("userInfo",JSON.toJSON(userinfoMapper.selectByUserPhone(userPhone)));
            }else{
                //认证失败
                jsonObject.put("message","error");
            }
        }else{
            //超时
            jsonObject.put("message","timeout");
        }
        return jsonObject;
    }

    @Override
    public Object checkRegisterWithSMS(String userPhone, String v_code) {
        long curTime = System.currentTimeMillis();
        Verificationcode verificationcode = verificationcodeMapper.selectByPrimaryKey(userPhone);
        long lastTime = Long.parseLong(verificationcode.getTime());

        JSONObject jsonObject = new JSONObject();

        if(curTime-lastTime<180000){
            //时间间隔不超过180s
            //验证有效
            //进行正确性验证
            if(userinfoMapper.selectByUserPhone(userPhone)!= null){
                jsonObject.put("message","error1");
            }
            else{
            if (v_code.equals(verificationcode.getV_code())){
                //认证成功
                jsonObject.put("message","success");
                //创建用户
                Userinfo userinfo = new Userinfo();
                userinfo.setUserName("手机用户"+userPhone);
                userinfo.setUserPhone(userPhone);
                userinfo.setPassword("bece78cf87e00ac28f0967880509c903");
                userinfo.setIsClient("Y");
                userinfo.setContractId(1);
                userinfo.setPermission(" ");
                userinfoMapper.insert(userinfo);
                jsonObject.put("userInfo",JSON.toJSON(userinfoMapper.selectByUserPhone(userPhone)));
            }else{
                //认证失败
                jsonObject.put("message","error");
            }}
        }else{
            //超时
            jsonObject.put("message","timeout");
        }
        return jsonObject;
    }

    @Override
    public Object checkRegisterWithSMSPC(String userPhone, String v_code,String userName,String password) {
        long curTime = System.currentTimeMillis();
        Verificationcode verificationcode = verificationcodeMapper.selectByPrimaryKey(userPhone);
        long lastTime = Long.parseLong(verificationcode.getTime());

        JSONObject jsonObject = new JSONObject();

        if(curTime-lastTime<180000){
            //时间间隔不超过180s
            //验证有效
            //进行正确性验证
            if(userinfoMapper.selectByUserPhone(userPhone)!= null){
                jsonObject.put("message","error1");
            }
            else{
                if (v_code.equals(verificationcode.getV_code())){
                    //认证成功
                    jsonObject.put("message","success");
                    //创建用户
                    Userinfo userinfo = new Userinfo();
                    userinfo.setUserName(userName);
                    userinfo.setUserPhone(userPhone);
                    userinfo.setPassword(MD5Encoder.getMD5(password));
                    userinfo.setIsClient("Y");
                    userinfo.setContractId(1);
                    userinfo.setPermission("11");
                    userinfoMapper.insert(userinfo);
                    jsonObject.put("userInfo",JSON.toJSON(userinfoMapper.selectByUserPhone(userPhone)));
                }else{
                    //认证失败
                    jsonObject.put("message","error");
                }}
        }else{
            //超时
            jsonObject.put("message","timeout");
        }
        return jsonObject;
    }
}
